﻿using System;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTab;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using System.IO;

namespace AC
{
	public static class Utility
	{
		public static object Flag = null;
		public static bool HasActive = false;
		public static string ActiveCode = "";

		private static Hashtable hstOldDisableControl = new Hashtable();

		public static void SetControlEnable(Control mainCtrl, bool enable)
		{
			if (enable == false)
				hstOldDisableControl.Clear();

			int length = mainCtrl.Controls.Count;
			Control ctrl = null;
			for (int i = 0; i < length; i++)
			{
				ctrl = mainCtrl.Controls[i];
				if (enable == false)
					if (ctrl.Enabled == false)
					{
						hstOldDisableControl.Add(ctrl.Name, true);
						continue;
					}

				if (enable)
					if (hstOldDisableControl.ContainsKey(ctrl.Name))
						continue;

				ctrl.Enabled = enable;
			}
		}

		public static void SetData(LookUpEdit lookUpEdit, object dataSource)
		{
			SetData(lookUpEdit, dataSource, 7, 7, TextEditStyles.DisableTextEditor);
		}

		public static void SetData(LookUpEdit lookUpEdit, object dataSource, int rowCount)
		{
			SetData(lookUpEdit, dataSource, rowCount, 7, TextEditStyles.DisableTextEditor);
		}

		public static void SetData(LookUpEdit lookUpEdit, object dataSource, int rowCount, TextEditStyles textEditStyles)
		{
			SetData(lookUpEdit, dataSource, rowCount, 7, textEditStyles);
		}

		public static void SetData(LookUpEdit lookUpEdit, object dataSource, int rowCount, int rowCountMax, TextEditStyles textEditStyles)
		{
			lookUpEdit.Properties.DataSource = dataSource;
			lookUpEdit.Properties.BestFitRowCount = (rowCount < rowCountMax ? rowCount : rowCountMax);
			lookUpEdit.Properties.DropDownRows = (rowCount < rowCountMax ? rowCount : rowCountMax);
			lookUpEdit.Properties.TextEditStyle = textEditStyles;
		}


		public static void SetData(RepositoryItemLookUpEdit repLookUpEdit, object dataSource)
		{
			SetData(repLookUpEdit, dataSource, 7, 7, TextEditStyles.DisableTextEditor);
		}

		public static void SetData(RepositoryItemLookUpEdit repLookUpEdit, object dataSource, int rowCount)
		{
			SetData(repLookUpEdit, dataSource, rowCount, 7, TextEditStyles.DisableTextEditor);
		}

		public static void SetData(RepositoryItemLookUpEdit repLookUpEdit, object dataSource, int rowCount, TextEditStyles textEditStyles)
		{
			SetData(repLookUpEdit, dataSource, rowCount, 7, textEditStyles);
		}

		public static void SetData(RepositoryItemLookUpEdit repLookUpEdit, object dataSource, int rowCount, int rowCountMax, TextEditStyles textEditStyles)
		{
			repLookUpEdit.DataSource = dataSource;
			repLookUpEdit.BestFitRowCount = (rowCount < rowCountMax ? rowCount : rowCountMax);
			repLookUpEdit.DropDownRows = (rowCount < rowCountMax ? rowCount : rowCountMax);
			repLookUpEdit.TextEditStyle = textEditStyles;
		}

		public static void SetData(CheckedComboBoxEdit cmb, object dataSource)
		{
			cmb.Properties.DataSource = dataSource;
			cmb.Properties.AutoHeight = true;
			cmb.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
		}

		public static void SetData(RepositoryItemCheckedComboBoxEdit cmb, object dataSource)
		{
			cmb.DataSource = dataSource;
			cmb.AutoHeight = true;
			cmb.TextEditStyle = TextEditStyles.DisableTextEditor;
		}

		//==================================================

		public static void InitControl(LookUpEdit lookUpEdit)
		{
			InitControl(lookUpEdit, "Key", "Value", "", null, false);
		}

		public static void InitControl(LookUpEdit lookUpEdit, object dataSource)
		{
			InitControl(lookUpEdit, "Key", "Value", "", dataSource, false);
		}

		public static void InitControl(LookUpEdit lookUpEdit, string nullText, object dataSource)
		{
			InitControl(lookUpEdit, "Key", "Value", nullText, dataSource, false);
		}

		public static void InitControl(LookUpEdit lookUpEdit, string valueMember, string displayMember)
		{
			InitControl(lookUpEdit, valueMember, displayMember, "", null, false);
		}

		public static void InitControl(LookUpEdit lookUpEdit, string valueMember, string displayMember, object dataSource)
		{
			InitControl(lookUpEdit, valueMember, displayMember, "", dataSource, false);
		}

		public static void InitControl(LookUpEdit lookUpEdit, string valueMember, string displayMember, string nullText, object dataSource, bool showHeader)
		{
			if (lookUpEdit.Properties.Columns.Count == 0)
				lookUpEdit.Properties.Columns.AddRange(new LookUpColumnInfo[] { new LookUpColumnInfo(valueMember, valueMember, 0, FormatType.None, "", false, HorzAlignment.Default), new LookUpColumnInfo(displayMember, 10, displayMember) });

			lookUpEdit.Properties.ValueMember = valueMember;
			lookUpEdit.Properties.DisplayMember = displayMember;

			lookUpEdit.Properties.NullText = nullText;
			lookUpEdit.Properties.NullValuePrompt = nullText;

			lookUpEdit.Properties.ShowFooter = false;
			lookUpEdit.Properties.ShowHeader = showHeader;
			lookUpEdit.Properties.AutoHeight = true;
			lookUpEdit.Properties.PopupSizeable = false;
			lookUpEdit.Properties.CloseUpKey = new KeyShortcut(System.Windows.Forms.Keys.Escape);
			lookUpEdit.Properties.PopupFormMinSize = new System.Drawing.Size(10, 10);
			lookUpEdit.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;
			lookUpEdit.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;

			if (dataSource != null)
			{
				lookUpEdit.Properties.DataSource = dataSource;

				int count = 0;

				try
				{
					Csla.NameValueListBase<int?, string> bd = (Csla.NameValueListBase<int?, string>)dataSource;
					count = bd.Count;
				}
				catch
				{
					count = 100;
				}

				if (count < 7)
					lookUpEdit.Properties.DropDownRows = count;
			}
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit)
		{
			InitControl(repLookUpEdit, "Key", "Value", "", null, false);
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit, object dataSource)
		{
			InitControl(repLookUpEdit, "Key", "Value", "", dataSource, false);
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit, string nullText, object dataSource)
		{
			InitControl(repLookUpEdit, "Key", "Value", nullText, dataSource, false);
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit, string valueMember, string displayMember)
		{
			InitControl(repLookUpEdit, valueMember, displayMember, "", null, false);
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit, string valueMember, string displayMember, object dataSource)
		{
			InitControl(repLookUpEdit, valueMember, displayMember, "", dataSource, false);
		}

		public static void InitControl(RepositoryItemLookUpEdit repLookUpEdit, string valueMember, string displayMember, string nullText, object dataSource, bool showHeader)
		{
			repLookUpEdit.AutoHeight = true;

			if (repLookUpEdit.Columns.Count == 0)
				repLookUpEdit.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] { new LookUpColumnInfo(valueMember, valueMember, 0, FormatType.None, "", false, HorzAlignment.Default), new LookUpColumnInfo(displayMember, 10, displayMember) });

			repLookUpEdit.ValueMember = valueMember;
			repLookUpEdit.DisplayMember = displayMember;

			repLookUpEdit.NullText = nullText;
			repLookUpEdit.NullValuePrompt = nullText;

			repLookUpEdit.ShowFooter = false;
			repLookUpEdit.ShowHeader = showHeader;

			repLookUpEdit.PopupSizeable = false;
			repLookUpEdit.CloseUpKey = new KeyShortcut(System.Windows.Forms.Keys.Escape);
			repLookUpEdit.PopupFormMinSize = new System.Drawing.Size(10, 10);
			repLookUpEdit.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;
			repLookUpEdit.TextEditStyle = TextEditStyles.DisableTextEditor;

			if (dataSource != null)
			{
				repLookUpEdit.DataSource = dataSource;

				int count = 0;

				try
				{
					Csla.NameValueListBase<int?, string> bd = (Csla.NameValueListBase<int?, string>)dataSource;
					count = bd.Count;
				}
				catch
				{
					count = 100;
				}

				if (count < 7)
					repLookUpEdit.DropDownRows = count;
			}
		}

		public static void InitControl(CheckedComboBoxEdit cmb)
		{
			cmb.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
			cmb.Properties.PopupSizeable = false;
			cmb.Properties.SelectAllItemCaption = "(Chọn tất cả)";
			cmb.Properties.ShowButtons = true;
			cmb.Properties.ShowPopupCloseButton = true;
			cmb.Properties.DisplayMember = "Value";
			cmb.Properties.ValueMember = "Key";
		}

		public static void InitControl(CheckedComboBoxEdit cmb, string valueMember, string displayMember)
		{
			cmb.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
			cmb.Properties.PopupSizeable = false;
			cmb.Properties.SelectAllItemCaption = "(Chọn tất cả)";
			cmb.Properties.ShowButtons = true;
			cmb.Properties.ShowPopupCloseButton = true;
			cmb.Properties.DisplayMember = displayMember;
			cmb.Properties.ValueMember = valueMember;
		}

		public static void InitControl(RepositoryItemCheckedComboBoxEdit cmb)
		{
			cmb.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
			cmb.PopupSizeable = false;
			cmb.SelectAllItemCaption = "(Chọn tất cả)";
			cmb.ShowButtons = true;
			cmb.ShowPopupCloseButton = true;
			cmb.DisplayMember = "Value";
			cmb.ValueMember = "Key";
		}

		public static void InitControl(RepositoryItemCheckedComboBoxEdit cmb, string valueMember, string displayMember)
		{
			cmb.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
			cmb.PopupSizeable = false;
			cmb.SelectAllItemCaption = "(Chọn tất cả)";
			cmb.ShowButtons = true;
			cmb.ShowPopupCloseButton = true;
			cmb.DisplayMember = displayMember;
			cmb.ValueMember = valueMember;
		}
		//-----------------------------

		public static object[] GetGroupValue(GridView gridView)
		{
			int rowHandle = gridView.GetDataRowHandleByGroupRowHandle(gridView.FocusedRowHandle);
			int i = 0;
			object[] groupValues = null;
			int groupColumnCount = gridView.GroupedColumns.Count;
			if (groupColumnCount > 0)
			{
				groupValues = new object[groupColumnCount];
				for (i = 0; i < groupColumnCount; i++)
					groupValues[i] = gridView.GetRowCellValue(rowHandle, gridView.GroupedColumns[i]);
			}
			return groupValues;
		}
	}
}
